﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using BXH.Movie.Model;
using Dapper;

namespace BXH.Movie.Dal
{
    public class UserInfoDal : BaseConnection
    {
        public UserInfo Login(string userCode, string password)
        {
            string sql = "select * from UserInfo where userCode=@UserCode and Password=@Password";
            return Conn.QuerySingleOrDefault<UserInfo>(sql, new { UserCode = userCode, Password = password });
        }

        public bool Add(UserInfo userInfo)
        {
            string sql = "insert into UserInfo values(@UserCode,@Password,@UserName,@UserNick,@Age,@Sex,@RoleId)";
            return Conn.Execute(sql, userInfo) > 0;
        }

        public bool Update(UserInfo userInfo) {
            string sql = @"Update  UserInfo set 
                            UserCode=@UserCode,
                            Password=@Password,
                            UserName=@UserName,
                            UserNick=@UserNick,
                            Age=@Age,
                            Sex=@Sex,
                            RoleId=@RoleId
                            where Id=@Id";
            return Conn.Execute(sql, userInfo) > 0;
        }

        public bool Delete(int id)
        {
            string sql = "delete from UserInfo where Id=@Id";
            return Conn.Execute(sql, new { Id = id })>0;
        }

        public IEnumerable<UserInfo> GetAdmins()
        {
            string sql = "select * from UserInfo where RoleId=@RoleId";
            return Conn.Query<UserInfo>(sql, new { RoleId = RoleType.Admin});
        }
    }
}
